/**
 * Project Name:DispenserClient.java
 * File Name:DispenserClient.java
 * Date:2019/4/18 10:45
 * Copyright (c) 2019, zhang.xiangyu@foxmail.com All Rights Reserved.
 */
package com.meatball.dispenser.client;

import com.alibaba.fastjson.JSON;
import com.meatball.utils.MD5Util;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: DispenserClient.java
 * @Description: todo(油机打通接口请求类)
 * @Author: 張翔宇
 * @Date: 2019/4/18 10:45
 * @Version: V1.0
 */
@Log4j2
@Component
public class DispenserClient {
    public static WebClient webClient = WebClient.create("http://180.101.146.23:8085/gsc");

    /**
     * @title: getDispenserData
     * @description: todo(获取中间件油机数据)
     * @params: []
     * @return: java.util.Map    返回类型
     * @author: 張翔宇
     * @date: 2019/4/18 13:31
     */
    public Mono<Map> getTestData() {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.set("password", "Yy123...");
        params.set("type", "account");
        params.set("userName", "minidolphin");
//        Map<String, String> params = new HashMap<>();
//        params.put("password", "123456");
//        params.put("type", "account");
//        params.put("userName", "13888888888");

        Mono<Map> result = webClient.post()
                .uri("/authenticate/login/adminlogin")
//                .contentType(MediaType.APPLICATION_JSON)
                .syncBody(params)
                .retrieve()
                .bodyToMono(Map.class);
        return result;
    }

    /**
     * @title: getDispenserData
     * @description: todo(获取中间件油机数据)
     * @params: []
     * @return: java.util.Map    返回类型
     * @author: 張翔宇
     * @date: 2019/4/18 13:31
     */
    public Mono<Map> getDispenserData() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String key="123456";
        String secret="123456";
        String sub_key="123456";
        String timestamp = sdf.format(new Date());
        Map map=new HashMap();
        map.put("id",1);
        String data= JSON.toJSONString(map);
        // 生成签名
        String str = secret + "data" + data + "key" + key + "sub_key" + sub_key + "timestamp" + timestamp + secret;
        String sign = MD5Util.md5Encrypt32Upper(str);
        // 发送参数
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.set("key", key);
        params.set("sub_key", sub_key);
        params.set("data", data);
        params.set("timestamp", timestamp);
        params.set("sign", sign);

//        String param="key="+key+"&sub_key="+sub_key+"&data="+data+"&timestamp="+timestamp+"&sign="+sign;

        Mono<Map> result = webClient.post()
                .uri("/queryOilDataByIdAndSubkey")
                .syncBody(params)
                .retrieve()
                .bodyToMono(Map.class);
        return result;
    }
}
